package com.game.spacetrader;

import java.util.ArrayList;

import com.game.spacetrader.TradeGoods.TradeGoodsView;
import com.game.spacetrader.data.Data;
import com.game.spacetrader.map.Coordinates;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.TextView;

/**
 * Class represents a list of universes
 *
 */
public class UniverseView extends Activity{
	TextView curLocation;
	/**
	 * Creates a listview of universes
	 * @param savedInstanceState bundle passed into the activity
	 */
	@Override
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
        setContentView(R.layout.universe_view);
        Log.d("OpenGeolocSearch","Start");
        curLocation = (TextView)findViewById(R.id.currLoc);
        setCurLoc();
        ArrayList<Planet> results = createPlanets();
        final ListView lv1 = (ListView) findViewById(R.id.ListView01);
		lv1.setAdapter(new UniverseBaseAdapter(this, results));

		lv1.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> a, View v, int position, long id) { 
				Log.d("OpenGeolocResult", "onItemClick");
				Object o = lv1.getItemAtPosition(position);
				Game.getInstance().setCurrentPlanet((Planet)o);
				Game.getInstance().setTravelTo((Planet)o);
				goToPlanetView();
			}
		});
        
	}
	
	/**
	 * Sets the current location for the player
	 */
	public void setCurLoc(){
		Coordinates c = Game.getInstance().getPlayer().getLocation();
		curLocation.setText("Current Location: ("+c.getX()+", "+c.getY()+")");
	}
	
	/**
	 * Goes to the view of a specific planet
	 */
	public void goToPlanetView(){
		Intent i = new Intent(this, PlanetView.class);
		startActivity(i);
	}
	
	/**
	 * Create the list of planets
	 */
	public ArrayList<Planet> createPlanets(){
		ArrayList<Planet> planets = new ArrayList<Planet>();
		Data d = new Data("universe.db");
		Planet tempP;
		String tName, tSystem;
		String data;
		String[] data2;
		int x,y,tech,res;
		for(int i = 0; i < d.getNumRows(); i++){
			data = d.execSQL("SELECT * FROM @TABLE WHERE ID=" + i)[0];
			data2=data.split(" ");
			tName=data2[0];
			tSystem=data2[1];
			x=Integer.parseInt(data2[2]);
			y=Integer.parseInt(data2[3]);
			tech=Integer.parseInt(data2[4]);
			res=Integer.parseInt(data2[5]);
			tempP = new Planet(tName,tSystem,x,y,tech,res);
			planets.add(tempP);
		}
		return planets;
	}
	
	/**
     * Listener for the options menu
     * @param item the item in the Menu that is clicked
     */
    @Override
	public boolean onOptionsItemSelected(MenuItem item) {
	    switch (item.getItemId()) {
	        case R.id.travel:
	        	Intent i = new Intent(this, UniverseView.class);
	        	startActivity(i);
	            return true;
	        case R.id.market:
	        	Intent i2 = new Intent(this, TradeGoodsView.class); startActivity(i2);
	        	return true;
	        default:
	            return super.onOptionsItemSelected(item);
	    }
	}
    
    @Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_space_trader, menu);
	    return true;
	}
}
